#ifndef _DATA_TRANSFER_H_
#define _DATA_TRANSFER_H_
#include <string.h>
#include <cstdint>
#include <stdint.h>

//#define PROTO_BIG_ENDIAN
#define PROTO_LITTLE_ENDIAN
union Int16Data
{
    int16_t _data;
    uint8_t _byte[2];
};

union Int32Data
{
    int32_t _data;
    uint8_t _byte[4];
};

static inline int16_t Byte2Short(const uint8_t* buf){
    Int16Data int16_data;
#ifdef LITTLE_ENDIAN
#ifdef PROTO_LITTLE_ENDIAN
    int16_data._byte[0] = buf[0];
    int16_data._byte[1] = buf[1];
#else
    int16_data._byte[1] = buf[0];
    int16_data._byte[0] = buf[1];
#endif
#else
#ifdef PROTO_LITTLE_ENDIAN
    int16_data._byte[1] = buf[0];
    int16_data._byte[0] = buf[1];
#else
    int16_data._byte[0] = buf[0];
    int16_data._byte[1] = buf[1];
#endif
#endif
    return int16_data._data;
}

static inline void Short2Byte(int16_t data, uint8_t* buf){
#ifdef LITTLE_ENDIAN
#ifdef PROTO_LITTLE_ENDIAN
    memcpy(buf, &data, 2);
#else
    buf[0] = data >> 8;
    buf[1] = data;
#endif
#else
#ifdef PROTO_LITTLE_ENDIAN
    buf[0] = data >> 8;
    buf[1] = data;
#else
    memcpy(buf, &data, 2);
#endif
#endif
}

static inline void Int322Byte(int32_t data, uint8_t* buf){
#ifdef LITTLE_ENDIAN
#ifdef PROTO_LITTLE_ENDIAN
    memcpy(buf, &data, 4);
#else
    buf[0] = data >> (8 * 3);
    buf[1] = data >> (8 * 2);
    buf[2] = data >> (8 * 1);
    buf[3] = data;
#endif
#else
#ifdef PROTO_LITTLE_ENDIAN
    buf[0] = data >> (8 * 3);
    buf[1] = data >> (8 * 2);
    buf[2] = data >> (8 * 1);
    buf[3] = data;
#else
    memcpy(bufm &data, 4);
#endif
#endif
}

static inline int32_t Byte2Int32(const uint8_t* buf){
    Int32Data data;
#ifdef LITTLE_ENDIAN
#ifdef PROTO_LITTLE_ENDIAN
    data._byte[0] = buf[0];
    data._byte[1] = buf[1];
    data._byte[2] = buf[2];
    data._byte[3] = buf[3];
#else
    data._byte[3] = buf[0];
    data._byte[2] = buf[1];
    data._byte[1] = buf[2];
    data._byte[0] = buf[3];
#endif
#else
#ifdef PROTO_LITTLE_ENDIAN
    data._byte[3] = buf[0];
    data._byte[2] = buf[1];
    data._byte[1] = buf[2];
    data._byte[0] = buf[3];
#else
    data._byte[0] = buf[0];
    data._byte[1] = buf[1];
    data._byte[2] = buf[2];
    data._byte[3] = buf[3];
#endif
#endif
    return data._data;
}

#endif
